﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace SupplyChainManagement
{
    public partial class InventoryItemStatus : Form
    {
        string index, status, name;
        InventoryDetail invDetail;

        public InventoryItemStatus(InventoryDetail inv, DataGridViewCellCollection s)
        {
            InitializeComponent();
            //for the dropdown menu
            this.cbStatus.Items.AddRange(new object[] {"ready",
                        "not ready"});
            invDetail = inv;
            index = s[0].Value.ToString();
            name = s[2].Value.ToString();
            lblName.Text = name;
            status = s[3].Value.ToString();
            cbStatus.Text = status;
            
        }

        private void ItemStatus_FormClosed(object sender, FormClosedEventArgs e)
        {
            invDetail.Enabled = true;
            invDetail.Activate();
            invDetail.LoadData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();

                OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                sqlCommand.CommandText = "update rawitem set item_status='" + cbStatus.Text
                    + "' where item_id='" + index + "'";
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Dispose();

                MessageBox.Show("Success");
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
                this.Close();
                invDetail.Enabled = true;
                invDetail.Activate();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            invDetail.Enabled = true;
            invDetail.Activate();
        }

    }
}
